package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.m_Bean;

public class introduction_servlet extends HttpServlet{
	   public void init(ServletConfig config) throws ServletException{
		      super.init(config);
		      try {  
		            Class.forName("com.mysql.jdbc.Driver");
		      }
		      catch(Exception e){} 
		   }
		  public String handleString(String s){
		      try{ 
		            byte bb[]=s.getBytes("iso-8859-1");
		            s=new String(bb);
		      }
		      catch(Exception ee){} 
		      return s;  
		   }
		public void doPost(HttpServletRequest request,HttpServletResponse response)
		                        throws ServletException,IOException{
		      Connection con; 
		      Statement sql; 
		      m_Bean reg=new m_Bean(); 
		      request.setAttribute("m_Bean",reg);
		      String poet=request.getParameter("poet");
		      String imagename=request.getParameter("imagename");
		      String introduction=request.getParameter("introduction"); 
		      try{ 
		    	   poet=handleString(poet);
		    	   introduction=handleString(introduction);
		    	   imagename=handleString(imagename);
		           String insertRecord="('"+imagename+"','"+poet+"','"+introduction+"')";
		           String uri="jdbc:mysql://127.0.0.1/poetry?"+
		                       "user=root&password=123&characterEncoding=gb2312";
		           con=DriverManager.getConnection(uri);
		           String insertCondition="insert into poet values "+insertRecord;
		           sql=con.createStatement();	
		           int m=sql.executeUpdate(insertCondition);
	                if(m!=0){
	                	reg.setPoet(poet);
	                    reg.setIntroduction(introduction);
	                }
	                con.close();
		      }
		      catch(SQLException e){
					System.out.println(e);
				}
		      response.sendRedirect("m_zjm.jsp");
		   }
		   public  void  doGet(HttpServletRequest request,HttpServletResponse response) 
		                        throws ServletException,IOException{
		      doPost(request,response);
		   }
		}
